import React from 'react';

class TodoForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      name: '',
    };

    // bind this
    this.handleNameInput = this.handleNameInput.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleNameInput(event) {
    this.setState({ name: event.target.value });
  }

  handleSubmit(event) {
    event.preventDefault();
    const name = this.state.name.trim();

    if (name) {
      this.props.onFormSubmit(name);
      this.setState({ name: '' });
    }
  }

  render() {
    return (
      <form className="todoForm" onSubmit={this.handleSubmit}>
        <input
          type="text"
          name="name"
          placeholder="What do you want to do today"
          value={this.state.name}
          onInput={this.handleNameInput}
        />
        <input type="submit" onSubmit={this.handleSubmit} value="Submit!" />
      </form>
    );
  }
}

TodoForm.propTypes = {
  onFormSubmit: React.PropTypes.func,
};

export default TodoForm;
